package com.sgxy.wmservice.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sgxy.commonutils.R;
import com.sgxy.wmservice.entity.WmGoods;
import com.sgxy.wmservice.entity.WmGoodsType;
import com.sgxy.wmservice.service.WmGoodsTypeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author testjava
 * @since 2022-04-29
 */
@RestController
@RequestMapping("/wmservice/wmgoodstype")
public class WmGoodsTypeController {
    @Autowired
    private WmGoodsTypeService wmGoodsTypeService;


    //获取分页列表
    @PostMapping("{page}/{limit}")
    public R index(@PathVariable Long page,
                   @PathVariable Long limit,
                   @RequestBody(required = false) WmGoodsType wmGoodsType){
        //System.out.println(1);
        Page<WmGoodsType> rolePage = new Page<>(page,limit);

        QueryWrapper<WmGoodsType> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(wmGoodsType.getTypeName())){
            wrapper.like("type_name", wmGoodsType.getTypeName());
        }
        wmGoodsTypeService.page(rolePage,wrapper);
        return R.ok().data("items",rolePage.getRecords()).data("total",rolePage.getTotal());
    }
    @GetMapping("getGoodsTypeAll")
    public R getRoleAll(){
        List<WmGoodsType> list = wmGoodsTypeService.list(null);
        return R.ok().data("list",list);
    }
    //根据id获取
    @GetMapping("getGoodsType/{id}")
    public R getGoodsType(@PathVariable String id){
        WmGoodsType wmGoodsType = wmGoodsTypeService.getById(id);
        return R.ok().data("wmGoodsType", wmGoodsType);
    }
    //更新信息
    @PostMapping("updateGoodsType")
    public R updateGoodsType(@RequestBody WmGoodsType wmGoodsType){
        wmGoodsTypeService.updateById(wmGoodsType);
        return R.ok();
    }
    //添加
    @PostMapping("addGoodsType")
    public R addGoodsType(@RequestBody WmGoodsType wmGoodsType){
//        System.out.println(aclRole);
        wmGoodsTypeService.save(wmGoodsType);
//        System.out.println(aclRole);
        return R.ok();
    }
    //删除
    @DeleteMapping("{id}")
    public R deleteGoodsTypeById(@PathVariable String id){
        boolean flag = wmGoodsTypeService.deleteRoleById(id);
        return flag?R.ok():R.error();
    }
}

